<?php
namespace app\index\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\Route;
Route::get('d/:dwz', 'index/index');  
class Index extends Base
{

 
    public function wxtoken()
    {
$appid= 'wx19d80ea725d45f87';
$secret='be81f31135b8aff5ba1364bb216589fc';
$access_tokendata= Db::name('accesstoken')->where('id',1)->find(); 
if($access_tokendata['time']<time()-$access_tokendata['expires_in']){
$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$urldata= file_get_contents($url);
$urldata=json_decode($urldata,true);
$access_token=$urldata['access_token'];   
$access_tokenup= Db::name('accesstoken')->where('id',1)->update(['access_token' =>$access_token,'time'=>time(),'expires_in'=>$urldata['expires_in']]);
}else{
$access_token =$access_tokendata['access_token'];
}
return $access_token;

    }
    public function index()
    {
        

    }

   public function api()
    {  
        

header('Content-type:text/html;charset=utf8');
require_once (root_path().'/extend/wechat/wx.php');

$appid="wx19d80ea725d45f87";//这里请填你真实的参数值
$appsecret="be81f31135b8aff5ba1364bb216589fc";//这里请填你真实的参数值
$token="weixin";//这里请填你真实的参数值

$wx=new \WeChat($appid,$appsecret,$token);

//验证url
// $wx->firstValid();

//判断消息类型
$result=$wx->responseMsg();

if($result['type']=='text'){
    //文本消息
    transmitText($result['obj'], '你发送的是文本，内容为：'.$result['obj']->Content);
}

}


/*
 * 接收文本消息
 */
function receiveText($object)
{
    $content = "你发送的是文本，内容为：".$object->Content;
    $result = $this->transmitText($object, $content);
    return $result;
}

/*
 * 回复文本消息
 */
function transmitText($object, $content)
{
    $textTpl = "<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[text]]></MsgType>
        <Content><![CDATA[%s]]></Content>
        </xml>";
    $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
    file_put_contents('./test.txt',$result);
    echo $result;die;
    // return $result;
}



}
  

  

